package edu.gatech.cc.liam.core;

public class Utils {

    public static double[][] reshape(double[][] A, int m, int n) {
        int origM = A.length;
        int origN = A[0].length;
        if(origM*origN != m*n){
            throw new IllegalArgumentException("New matrix must be of same area as matix A");
        }
        double[] A1D = new double[A.length * A[0].length];
        int index = 0;
        for(int i = 0;i<A.length;i++){
            for(int j = 0;j<A[0].length;j++){
                A1D[index++] = A[i][j];
            }
        }
        return vecToMatrix(A1D, m, n);
    }
    
    public static double[][] vecToMatrix(double[] A, int m, int n) {
        if(A.length != m*n){
            throw new IllegalArgumentException("New matrix must be of same area as A");
        }
    	double[][] B = new double[m][n];
        int index = 0;
        for(int i = 0;i<m;i++){
            for(int j = 0;j<n;j++){
                B[i][j] = A[index++];
            }

        }
        return B;
    }
    
    public static double[] MatrixToVec(double[][] A) {
    	double[] v = new double[A.length * A[0].length];
        for(int i = 0;i<A.length;i++){
        	assert A[i].length == A[0].length;
            for(int j = 0;j<A[i].length;j++){
               v[i*A[0].length + j] = A[i][j];
            }
        }
        return v;
    }
}
